package com.xdap.common.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * 配置文件读取
 *
 * @Author: JCccc
 * @Des: ElegantDay
 */

@Configuration
public class ComPluginPropertiesUtil {
    private static Logger logger = LoggerFactory.getLogger(ComPluginPropertiesUtil.class);

    private static final String ENVIRONMENT_FILE_NAME = "application-environment.properties";
    private static final String FILE_NAME = "application.properties";
    private static Properties props;
    private static Properties propsEnvironment;
    private static String environmentName;
    @Value("application-${spring.application.name}")
    private static String fileName;

    static {
        props = new Properties();
        propsEnvironment = new Properties();
        try {
            propsEnvironment.load(new InputStreamReader(
                    ComPluginPropertiesUtil.class.getClassLoader().getResourceAsStream(ENVIRONMENT_FILE_NAME), "UTF-8"));
            environmentName = propsEnvironment.getProperty("this.evironment"); // 环境名称
            logger.info("\n\n\n读取到的配置文件环境为为{}\n\n\n", environmentName);
        } catch (Exception e) {
            logger.error("配置this.evironment文件读取异常{}", e);
        }
        // 因为先加载static再加载@value的值，所以直接读取文件内的名称
        try {
            propsEnvironment.load(new InputStreamReader(
                    ComPluginPropertiesUtil.class.getClassLoader().getResourceAsStream(FILE_NAME), "UTF-8"));
            String activeName = propsEnvironment.getProperty("spring.profiles.active"); // 环境名称
            if (!StringUtils.isEmpty(activeName)) {
                propsEnvironment.load(new InputStreamReader(
                        ComPluginPropertiesUtil.class.getClassLoader().getResourceAsStream("application-" + activeName +
                                ".properties"), "UTF-8"));
            }
            fileName = "application-" + propsEnvironment.getProperty("spring.application.name"); // 应用名称

            logger.info("\n\n\n读取到的配置文件为{}\n\n\n", fileName);

        } catch (Exception e) {
            logger.error("读取文件spring.application.name读取异常{}", e);
        }
        try {
            String fileDefault = fileName+"-" + environmentName + ".properties";
            InputStream resourceAsStream = ComPluginPropertiesUtil.class.getClassLoader()
                    .getResourceAsStream(fileDefault);
            props.load(new InputStreamReader(resourceAsStream, "UTF-8"));
            logger.info("\n\n\n读取到的配置文件为{}\n\n\n", fileName);
        } catch (Exception e) {
            logger.error("配置文件{}读取不到，属于正常报错，可忽略：{}", fileName, e);
        }
    }

    /**
     * @USER liShuShu
     * @TIME 2023/2/14 0014 16:46
     * @DESC 获取公共配置文件
     **/
    public static String getProperty(String key) {
        String value = props.getProperty(key.trim());
        if (StringUtils.isEmpty(value)) {
            return null;
        }
        return value.trim();
    }


    public static String getEnvironmentName() {
        return environmentName;
    }

}